var $=function(id){return document.getElementById(id);}
var Softdown = {
		//存储dom
		download:$('softdownload'),
		init:function(){
			this.download.addEventListener('click',function(){
				var software = $('software').value;
				var xhr = new XMLHttpRequest();
				xhr.open("GET", "http://local.cms.com/Chrome_Extenstion_1Mobile/test.php?software=" + software, true);
				xhr.onreadystatechange = function() {
  					if (xhr.readyState == 4) {
    					//innerText 不会让攻击者插入 HTML 元素。
    					$("msg").innerText = xhr.responseText;
						chrome.downloads.download({url:xhr.responseText});
  					}
				}
				xhr.send();
			},true);
		},
		apk_downloader:function(){
			$('apk_downloader').addEventListener('click',function(){
				chrome.tabs.create({url:'apkdownload.html'},function(tab){
					
				});
			},false);
		},
		
		wish_list:function(){
			$('wish_list').addEventListener('click',function(){
				chrome.tabs.create({url:'wish.html'},function(tab){
					
				});
			},false);
		},
		
		
		clickbutton:function(){
			$('testbutton').addEventListener('click',function(){
				var path = chrome.extension.getURL("wish.html");
				//var url = chrome.runtime.getURL();
				//alert(path);
				window.open(path);
			},false);
		},
		receive_message:function(){
			$('receive_mes').addEventListener('click',function(){
				receive_mes();
			},false);
		},
		testtmp:function(){
			$('testtmp').addEventListener('click',function(){
				test_tmp();
			},false);
		}
}

function receive_mes(){
	alert('bbbb');
	
	chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
		chrome.tabs.sendMessage(tabs[0].id, {greeting: "您好"}, function(response) {
    		//console.log(response.farewell);
    		console.log('aaaaaaaa');
  		});
	});
}

function test_tmp(){
	alert('bbbbb');
	
	//chrome.tabs.create({url:"http://www.1mobile.com"}, function(cur_tab){
		//var cur_url = cur_tab.url;
		//console.log(cur_url);
	//});
	
	chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
		alert('ccccc');
		alert(tabs.length);
		var cur_url = tabs[0].url;
		alert(cur_url);
	});
	
	
	//chrome.tabs.getCurrent(function(cur_tab){
		//alert(cur_tab);
		//var cur_url = cur_tab.url;
		//alert(cur_url);
	//});
}

document.addEventListener('DOMContentLoaded', function () {
  Softdown.init();
  Softdown.clickbutton();
  Softdown.receive_message();
  Softdown.apk_downloader();
  Softdown.wish_list();
  Softdown.testtmp();
  
});
